﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super_Wall_Breaker.Sources.GameElements.Bricks;
using Super_Wall_Breaker.Sources.GameEngine;
using Microsoft.Xna.Framework;

namespace Super_Wall_Breaker.Sources.Factories
{
    /// <summary>
    /// This static class creates a wall, ie a list of bricks.
    /// </summary>
    public static class WallFactory
    {
        /// <summary>
        /// This function creates a basic wall, ie a five line of bricks.
        /// </summary>
        /// <returns>A list of bricks.</returns>
        public static List<Brick> createBasicWall(){

            Random rand = new Random();
            GlobalConfiguration config = GlobalConfiguration.Configuration;
            int yMid = config.ScreenHeight / 2;            

            List<Brick> wall = new List<Brick>();

            WallFactory.addLine(wall, yMid - 2 * GlobalConfiguration.BRICK_TEXTURE_HEIGHT, 18, GlobalConfiguration.BRICK_TEXTURE_HEIGHT, GlobalConfiguration.BRICK_TEXTURE_WIDTH, GlobalConfiguration.BRICK_TEXTURE_WIDTH + 10, 1, rand);
            WallFactory.addLine(wall, yMid - GlobalConfiguration.BRICK_TEXTURE_HEIGHT, 19, GlobalConfiguration.BRICK_TEXTURE_HEIGHT, GlobalConfiguration.BRICK_TEXTURE_WIDTH, GlobalConfiguration.BRICK_TEXTURE_WIDTH / 2 + 10, 1, rand);
            WallFactory.addLine(wall, yMid, 20, GlobalConfiguration.BRICK_TEXTURE_HEIGHT, GlobalConfiguration.BRICK_TEXTURE_WIDTH, 10, 1, rand);
            WallFactory.addLine(wall, yMid + GlobalConfiguration.BRICK_TEXTURE_HEIGHT, 19, GlobalConfiguration.BRICK_TEXTURE_HEIGHT, GlobalConfiguration.BRICK_TEXTURE_WIDTH, GlobalConfiguration.BRICK_TEXTURE_WIDTH / 2 + 10, 1,rand);
            WallFactory.addLine(wall, yMid + 2 * GlobalConfiguration.BRICK_TEXTURE_HEIGHT, 18, GlobalConfiguration.BRICK_TEXTURE_HEIGHT, GlobalConfiguration.BRICK_TEXTURE_WIDTH, GlobalConfiguration.BRICK_TEXTURE_WIDTH + 10, 1,rand);

            return wall;
        }

        /// <summary>
        /// This function creates a line of bricks.
        /// </summary>
        /// <param name="wall">The list of bricks.</param>
        /// <param name="yMid">Y position in the mid of the bricks line</param>
        /// <param name="brickNumber">Number of bricks</param>
        /// <param name="brickHeight">Height of bricks</param>
        /// <param name="brickWidth">Width of bricks</param>
        /// <param name="xOffset">offset on X axis</param>
        /// <param name="step">number of pixels between two bricks</param>
        /// <param name="random">The random number generator.</param>
        private static void addLine(List<Brick> wall,int yMid, int brickNumber ,int brickHeight, int brickWidth,  int xOffset, int step,Random random)
        {
            Vector2 topLeftCorner = new Vector2(xOffset, yMid - brickHeight / 2);                       

            for (int i = 0; i < brickNumber; i++)
            {
                int life = random.Next(1, 6); // random life, between 1 and 5 (includes)
                Color color = BrickFactory.GetBrickColor(life);

                Brick brick = new BasicBrick(life, new Vector2(xOffset + i * brickWidth + step, yMid - brickHeight / 2), new Rectangle(0, 51, brickWidth, brickHeight));
                brick.Color = color;
                brick.Break.Color = color;
                wall.Add(brick);
            }

        }
    }

}
